﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;
using System.Windows.Controls.Primitives;
using System.Windows.Interactivity;
using RestaurantBooker.Controls;
using RestaurantBooker.Model.Enums;

namespace RestaurantBooker.View
{
    /// <summary>
    /// Container for child window with some overriden behavior
    /// </summary>
    /// 
      //  [StyleTypedProperty(Property = "PreviewStyle", StyleTargetType = typeof(Control))]
    public partial class ChildWindowContainer : ChildWindow
    {
        #region [ Private fields ]
        private bool _isOpened = false;
        #endregion

        #region [ Helper methods ]
        public static void CloseWindow(FrameworkElement control, DialogResultEx dialogResult)
        {
            RestaurantBooker.Helpers.ChildWindowHelper.CloseWindow(control, dialogResult);            
        }
        #endregion

        #region [ Constructor ]
        public ChildWindowContainer()
        {            
            InitializeComponent();
            //HACK: this special case for ChldWindow
            this.Style = Application.Current.Resources["AppChildWindowContainerStyle"] as Style;
            this.Padding = new Thickness(5);
        }
        #endregion

        #region [ Overriden methods ]
        protected override void OnOpened()
        {
            base.OnOpened();
            _isOpened = true;
        }


        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            _isOpened = false;
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                UIElement el = FocusManager.GetFocusedElement() as UIElement;
                if (el != null && el is ComboBox && (el as ComboBox).IsDropDownOpen)
                    base.OnKeyUp(e);
                else
                    DialogResult = false;
            }
            else
                base.OnKeyUp(e);
        }
        #endregion

        #region [ Public properties ]
        public bool DialogIsOpened
        {
            get
            {
                return _isOpened;
            }
        }
        #endregion
    }
}

